/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package View;

import controleur.AbstractChatControleur;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.net.SocketException;
import java.net.UnknownHostException;
import java.util.logging.Level;
import java.util.logging.Logger;
import model.Connection;
import javax.swing.JFrame;
import javax.swing.JList;
import javax.swing.JMenuItem;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;

/**
 *
 * @author Matthieu
 */
public class ListListener implements MouseListener
{
    AbstractChatControleur Controleur;
    MainFrame mainfame;

    public ListListener(AbstractChatControleur Controleur) 
    {
        this.Controleur = Controleur;
    }
    
    public ListListener(AbstractChatControleur Controleur, MainFrame mainfame) 
    {
        this.Controleur = Controleur;
        this.mainfame = mainfame;
    }
    
   
    public void valueChanged(ListSelectionEvent e) 
    {
        JList item = (JList) e.getSource();
        
        if ("listeattente".equals(item.getName())){
            System.out.println("Clique sur une connection");
        }
        
    }

    @Override
    public void mouseClicked(MouseEvent e) {
        
        if(e.getClickCount() == 2)                                              // Double clique sur une conection
        {
            Object connection = mainfame.getListeattenteSelection();            // Recupere la conection selectionner
            System.out.println(connection);
            
            if ( connection instanceof Connection)
            {
                try 
                {
                    ((Connection)connection).start();                           // Demmarre l'audio et les sockets
                } 
                catch (SocketException ex) { Logger.getLogger(ListListener.class.getName()).log(Level.SEVERE, null, ex); } 
                catch (UnknownHostException ex) { Logger.getLogger(ListListener.class.getName()).log(Level.SEVERE, null, ex); }
            }
        }
    }

    @Override
    public void mousePressed(MouseEvent e) { }

    @Override
    public void mouseReleased(MouseEvent e) { }

    @Override
    public void mouseEntered(MouseEvent e) { }

    @Override
    public void mouseExited(MouseEvent e) { }

    
    
    
}
